﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Collections;

namespace BHair
{
    public partial class frmpz : Form
    {
        public frmpz()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string fwq = this.txtfwq.Text;
            string sjk = "master";
            string sa = this.txtsa.Text;
            string pass = this.txtpass.Text;
            string sqlstr = string.Format(@"Data Source={0};Initial Catalog={1};User ID={2};Password={3}",fwq,sjk,sa,pass);
            SqlConnection conn = new SqlConnection(sqlstr);
            try
            {
                conn.Open();
                MessageBox.Show("保存参数成功");
                save();
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存参数失败，请检查数据库配置"+ex.Message);
            }
            conn.Close();

        }
        /*
         * 创建写入配置文件方法
         *
         * */
        public void save() 
        {
            //获取窗体字段
            string fwq = this.txtfwq.Text;
            string sjk = "BHairDB";
            string sa = this.txtsa.Text;
            string pass = this.txtpass.Text;
            //声明Configuration
            System.Configuration.Configuration cf = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
            //清空ConnectionStrings
            cf.ConnectionStrings.ConnectionStrings.Clear();
            //声明 ConnectionStringSettings
            ConnectionStringSettings s = new ConnectionStringSettings();
            //为 ConnectionStringSettings赋值
            s.ProviderName = "System.Data.SqlClient";
             s.ConnectionString = string.Format(@"Data Source={0};Initial Catalog={1};User ID={2};Password={3}",fwq,sjk,sa,pass);//连接语句
             s.Name = "BHair.Properties.Settings.BHairDBConnectionString";//名称
             cf.ConnectionStrings.ConnectionStrings.Add(s);
             cf.Save();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try 
            {
                label2.Visible = true;
                string sqlconn =string.Format( "Data Source={0};Initial Catalog=master;User ID={1};PWD={2}",txtfwq.Text,txtsa.Text,txtpass.Text);
                string sql = "";
                string mdbfile = AppDomain.CurrentDomain.BaseDirectory + @"data\BHairDB.mdf";
                string logfile = AppDomain.CurrentDomain.BaseDirectory + @"data\BHairDB_log.ldf";
                if (File.Exists(mdbfile)) 
                {
                    sql = "EXEC sp_attach_db   @dbname   ='BHairDB',@filename1 = '" + mdbfile + "',@filename2='" + logfile + "'";
                    // string sql1 = string.Format("INSERT INTO bb (版本) VALUES ('{0}')", DateTime.Now.AddDays(15).ToShortDateString().ToString());
                    string sql1 = string.Format("INSERT INTO [bb] (版本) VALUES ('{0}')", DateTime.Now.AddDays(15).ToShortDateString().ToString());

                    SqlConnection conn = new SqlConnection(sqlconn);
                    SqlConnection conn1 = new SqlConnection(sqlconn);
                    using (conn) 
                    {
                        lock (conn) 
                        {
                            conn.Open();
                            SqlCommand comm = new SqlCommand(sql,conn);
                            comm.ExecuteNonQuery();
                            conn1.Open();
                            MessageBox.Show(DateTime.Now.AddDays(15).ToShortDateString());
                            
                            SqlCommand comm1 = new SqlCommand(sql1,conn1);
                            comm1.ExecuteNonQuery();
                            MessageBox.Show("创建数据库成功,请重新运行软件");
                        }
                    }
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void frmpz_Load(object sender, EventArgs e)
        {

        }
        /*
         * 创建数据库
         * */
       
        public void ExecuteSQLFile(String sqlFileName)

        {

            SqlConnection connecction = null;

            try

            {
                string sqlconn = string.Format("Data Source={0};Initial Catalog=master;User ID={1};PWD={2}", txtfwq.Text, txtsa.Text, txtpass.Text);
                connecction = new SqlConnection(sqlconn);

                SqlCommand command = connecction.CreateCommand();

                connecction.Open();



                FileStream stream = new FileStream(sqlFileName, FileMode.Open);

                StreamReader reader = new StreamReader(stream);



                StringBuilder builder = new StringBuilder();

                String strLine = "";

                while ((strLine = reader.ReadLine()) != null)

                {

                    if (strLine.Trim().ToUpper() != @"GO")

                    {

                        builder.AppendLine(strLine);

                    }

                    else

                    {

                        command.CommandText = builder.ToString();

                        command.ExecuteNonQuery();

                        builder.Remove(0, builder.Length);

                    }

                }



            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

            finally

            {

                if (connecction != null && connecction.State != ConnectionState.Closed)

                {

                    connecction.Close();

                }

            }

        }
        public void cj() 
        {
            string sql1 = "USE [master]";
            string sql2 = "CREATE DATABASE [BHairDB] ON  PRIMARY"
                +@"( NAME = N'BHairDB', FILENAME = N'D:\BHairDB.mdf' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )"
                + " LOG ON "
                +@"( NAME = N'BHairDB_log', FILENAME = N'd:\BHairDB_log.ldf' , SIZE = 4224KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)";
            string sql3 = "ALTER DATABASE [BHairDB] SET ANSI_NULL_DEFAULT OFF";
            string sql4 = "ALTER DATABASE [BHairDB] SET ANSI_NULLS OFF";
            string sql5="ALTER DATABASE [BHairDB] SET ANSI_PADDING OFF";
            string sql6 = "ALTER DATABASE [BHairDB] SET ANSI_WARNINGS OFF";
            string sql7 = "ALTER DATABASE [BHairDB] SET ARITHABORT OFF";
            string sql8 = "ALTER DATABASE [BHairDB] SET AUTO_CLOSE OFF";
            string sql9 = "ALTER DATABASE [BHairDB] SET AUTO_CREATE_STATISTICS ON";
            string sql10 = "ALTER DATABASE [BHairDB] SET AUTO_SHRINK OFF";
            string sql11 = "ALTER DATABASE [BHairDB] SET AUTO_UPDATE_STATISTICS ON";
            string sql12 = "ALTER DATABASE [BHairDB] SET CURSOR_CLOSE_ON_COMMIT OFF";
            string sql13 = "ALTER DATABASE [BHairDB] SET CURSOR_DEFAULT  GLOBAL";
            string sql14 = "ALTER DATABASE [BHairDB] SET CONCAT_NULL_YIELDS_NULL OFF";
            string sql15 = "ALTER DATABASE [BHairDB] SET NUMERIC_ROUNDABORT OFF";
            string sql16 = "ALTER DATABASE [BHairDB] SET QUOTED_IDENTIFIER OFF";
            string sql17 = "ALTER DATABASE [BHairDB] SET RECURSIVE_TRIGGERS OFF";
            string sql18 = "ALTER DATABASE [BHairDB] SET  READ_WRITE";
            string sql19 = "ALTER DATABASE [BHairDB] SET RECOVERY FULL";
            string sql20 = "ALTER DATABASE [BHairDB] SET  MULTI_USER";
            string sql21 = "if ( ((@@microsoftversion / power(2, 24) = 8) and (@@microsoftversion & 0xffff >= 760)) or "
		    +"(@@microsoftversion / power(2, 24) >= 9) )begin "
	        +"exec dbo.sp_dboption @dbname =  N'BHairDB', @optname = 'db chaining', @optvalue = 'OFF'"
            +" end";
            string sql22 = "USE [BHairDB]";
            string sql23 = "SET ANSI_NULLS ON";
            string sql24 = "SET QUOTED_IDENTIFIER ON";
            string sql25 = "SET ANSI_PADDING ON";
            string sql26 = "CREATE TABLE [dbo].[wastebook]("
                + "	[wbid] [decimal](18, 0) IDENTITY(1,1) NOT NULL,"
                + "	[wbsid] [int] NULL,"
                + "	[wbincome] [money] NULL,"
                + "	[wbexpend] [money] NULL,"
                + "	[wbdate] [datetime] NULL,"
                + "	[wbremark] [nvarchar](100) NULL,"
                + "	[wbtype] [char](1) NULL,"
                + " CONSTRAINT [PK_wastebook] PRIMARY KEY CLUSTERED "
                + "("
                + "	[wbid] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql28 = "SET ANSI_PADDING OFF";
            string sql29 = "SET ANSI_NULLS ON";
            string sql30 = "SET QUOTED_IDENTIFIER ON";
            string sql31 = "CREATE TABLE [dbo].[sysconfig]("
                + "	[varindex] [int] NOT NULL,"
                + "	[varname] [nvarchar](20) NULL,"
                + "	[vartype] [nvarchar](10) NULL,"
                + "	[varvalue] [nvarchar](20) NULL,"
                + "	[varinfo] [nvarchar](100) NULL,"
                + " CONSTRAINT [PK_sysconfig] PRIMARY KEY CLUSTERED "
                + "("
                + "	[varindex] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql32 = "SET ANSI_NULLS ON";
            string sql33 = "SET QUOTED_IDENTIFIER ON";
            string sql34 = "CREATE TABLE [dbo].[subject]("
                + "	[sid] [int] IDENTITY(100,1) NOT NULL,"
                + "	[sname] [nvarchar](30) NULL,"
                + "	[stype] [tinyint] NULL,"
                + "	[sreadme] [nvarchar](200) NULL,"
                + "	[senabled] [tinyint] NULL,"
                + " CONSTRAINT [PK_subject] PRIMARY KEY CLUSTERED "
                + "("
                + "	[sid] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql35 = "SET ANSI_NULLS ON";
            string sql36 = "SET QUOTED_IDENTIFIER ON";
            string sql37 = "CREATE TABLE [dbo].[post]("
                + "	[pid] [int] IDENTITY(1,1) NOT NULL,"
                + "	[pname] [nvarchar](30) NULL,"
                + "	[preadme] [nvarchar](300) NULL,"
                + "	[pmode] [tinyint] NULL,"
                + "	[psort] [tinyint] NULL,"
                + "	[psalary] [smallmoney] NULL,"
                + "	[pbonus] [float] NULL,"
                + " CONSTRAINT [PK_post] PRIMARY KEY CLUSTERED "
                + "("
                + "	[pid] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql38 = "SET ANSI_NULLS ON";
            string sql39 = "SET QUOTED_IDENTIFIER ON";
            string sql40 = "CREATE TABLE [dbo].[pays]("
                + "	[pid] [nvarchar](10) NOT NULL,"
                + "	[pmid] [nvarchar](20) NULL,"
                + "	[peid1] [int] NULL,"
                + "	[peid2] [int] NULL,"
                + "	[peid3] [int] NULL,"
                + "	[pmoney] [money] NULL,"
                + "	[pcash] [money] NULL,"
                + "	[pzero] [money] NULL,"
                + "	[ptype] [tinyint] NULL,"
                + "	[pdate] [datetime] NULL,"
                + "	[premark] [nvarchar](500) NULL,"
                + "	[pstatus] [tinyint] NULL,"
                + "	[pcall] [tinyint] NULL,"
                + " CONSTRAINT [PK_pays] PRIMARY KEY CLUSTERED "
                + "("
                + "	[pid] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql41 = "SET ANSI_NULLS ON";
            string sql42 = "SET QUOTED_IDENTIFIER ON";
            string sql43 = "CREATE TABLE [dbo].[paydetails]("
                + "	[pdid] [decimal](18, 0) IDENTITY(1,1) NOT NULL,"
                + "	[pdpid] [nvarchar](10) NULL,"
                + "	[pdiid] [int] NULL,"
                + "	[pdnum] [tinyint] NULL,"
                + "	[pbonus] [float] NULL,"
                + "	[pmoney] [money] NULL,"
                + " CONSTRAINT [PK_paydetails] PRIMARY KEY CLUSTERED "
                + "("
                + "	[pdid] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql44 = "SET ANSI_NULLS ON";
            string sql45 = "SET QUOTED_IDENTIFIER ON";
            string sql46 = "CREATE TABLE [dbo].[member]("
                + "	[mid] [nvarchar](12) NOT NULL,"
                + "	[mname] [nvarchar](30) NULL,"
                + "	[mstatus] [tinyint] NULL,"
                + "	[mcid] [int] NULL,"
                + "	[mpass] [nvarchar](32) NULL,"
                + "	[mremark] [nvarchar](400) NULL,"
                + "	[msex] [tinyint] NULL,"
                + "	[mmonth] [tinyint] NULL,"
                + "	[mday] [tinyint] NULL,"
                + " [mphone] [nvarchar](13) NULL,"
                + "	[maddress] [nvarchar](100) NULL,"
                + "	[mother] [nvarchar](400) NULL,"
                + "	[mjoin] [datetime] NULL,"
                + "	[midcard] [nvarchar](18) NULL,"
                + "	[mbalance] [money] NULL,"
                + "	[mpoint] [int] NULL,"
                + "	[mlastime] [datetime] NULL,"
                + " CONSTRAINT [PK_member] PRIMARY KEY CLUSTERED "
                + "("
                + "	[mid] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql47 = "SET ANSI_NULLS ON";
            string sql48 = "SET QUOTED_IDENTIFIER ON";
            string sql49 = "CREATE TABLE [dbo].[LoginUser]("
                + "	[LoginUserID] [nvarchar](50) NOT NULL,"
                + "	[LoginPassword] [nvarchar](50) NULL,"
                + " CONSTRAINT [PK_LoginUser] PRIMARY KEY CLUSTERED "
                + "("
                + "	[LoginUserID] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql50 = "SET ANSI_NULLS ON";
            string sql51 = "SET QUOTED_IDENTIFIER ON";
            string sql52 = "CREATE TABLE [dbo].[jh]("
                + "	[djid] [nvarchar](50) NOT NULL,"
                + "	[spid] [int] NULL,"
                + "	[spname] [nvarchar](50) NOT NULL,"
                + "	[spjhj] [money] NOT NULL,"
                + "	[spjhs] [decimal](18, 0) NULL,"
                + "	[spjhtime] [nvarchar](50) NULL,"
                + "	[spgys] [nvarchar](50) NULL,"
                + "	[spjsr] [nvarchar](50) NULL,"
                + "	[spjhje] [money] NULL,"
                + "	[spjhbz] [nvarchar](50) NULL"
                + ") ON [PRIMARY]";
            string sql53 = "SET ANSI_NULLS ON";
            string sql54 = "SET QUOTED_IDENTIFIER ON";
            string sql55 = "CREATE TABLE [dbo].[items]("
                + "	[iid] [int] IDENTITY(1000,1) NOT NULL,"
                + "	[iname] [nvarchar](30) NULL,"
                + "	[ifprice] [smallmoney] NULL,"
                + "	[iuprice] [smallmoney] NULL,"
                + "	[inprice] [smallmoney] NULL,"
                + "	[iamount] [smallint] NULL,"
                + "	[imoney] [smallmoney] NULL,"
                + "	[icmoney] [smallmoney] NULL,"
                + "	[itype] [tinyint] NULL,"
                + "	[iconvert] [int] NULL,"
                + "	[ireadme] [nvarchar](300) NULL,"
                + "	[istatus] [tinyint] NULL,"
                + "	[isbonus] [tinyint] NULL,"
                + "	[isticheng] [tinyint] NULL,[jc] [int] NULL,"
                + " CONSTRAINT [PK_items] PRIMARY KEY CLUSTERED "
                + "("
                + "	[iid] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql56 = "SET ANSI_NULLS ON";
            string sql57 = "SET QUOTED_IDENTIFIER ON";
            string sql58 = "CREATE TABLE [dbo].[gys]("
                + "	[id] [int] IDENTITY(1,1) NOT NULL,"
                + "	[gysID] [int] NULL,"
                + "	[gysname] [nvarchar](50) NOT NULL,"
                + "	[gysaddress] [nvarchar](50) NULL,"
                + "	[gysphone] [nvarchar](50) NULL,"
                + "	[gysbz] [nvarchar](50) NULL,"
                + "	[gyslxr] [nvarchar](50) NULL,"
                + " CONSTRAINT [PK_gys] PRIMARY KEY CLUSTERED "
                + "("
                + "	[id] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql59 = "SET ANSI_NULLS ON";
            string sql60 = "SET QUOTED_IDENTIFIER ON";
            string sql61 = "CREATE TABLE [dbo].[employee]("
                + "	[eid] [int] IDENTITY(1,1) NOT NULL,"
                + "	[ecode] [tinyint] NULL,"
                + "	[ename] [nvarchar](30) NULL,"
                + "	[esex] [tinyint] NULL,"
                + "	[ephone] [nvarchar](15) NULL,"
                + "	[epid] [int] NULL,"
                + "	[esalary] [money] NULL,"
                + "	[ebonus] [float] NULL,"
                + "	[einput] [nvarchar](10) NULL,"
                + "	[eoutput] [nvarchar](10) NULL,"
                + "	[estatus] [tinyint] NULL,"
                + "	[epassword] [nvarchar](32) NULL,"
                + "	[eremark] [nvarchar](300) NULL,"
                + "	[eaddress] [nvarchar](100) NULL,"
                + "	[ecard] [nvarchar](18) NULL,"
                + "	[emobile] [nvarchar](11) NULL,"
                + " CONSTRAINT [PK_employee] PRIMARY KEY CLUSTERED "
                + "("
                + "	[eid] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql62 = "SET ANSI_NULLS ON";
            string sql63 = "SET QUOTED_IDENTIFIER ON";
            string sql64 = "CREATE TABLE [dbo].[emp_money]("
                + "	[emid] [decimal](18, 0) IDENTITY(1,1) NOT NULL,"
                + "	[emmoney] [money] NULL,"
                + "	[emtype] [tinyint] NULL,"
                + "	[emremark] [nvarchar](500) NULL,"
                + "	[emtime] [datetime] NULL,"
                + "	[empid] [int] NULL,"
                + " CONSTRAINT [PK_emp_money] PRIMARY KEY CLUSTERED "
                + "("
                + "	[emid] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql65 = "SET ANSI_NULLS ON";
            string sql66 = "SET QUOTED_IDENTIFIER ON";
            string sql67 = "CREATE TABLE [dbo].[deposit]("
                + "	[did] [decimal](18, 0) IDENTITY(1,1) NOT NULL,"
                + "	[dmid] [nvarchar](12) NULL,"
                + "	[dmoney] [money] NOT NULL,"
                + "	[ddate] [datetime] NULL,"
                + "	[dremark] [nvarchar](100) NULL,"
                + "	[dmode] [tinyint] NULL,"
                + "	[dparentid] [decimal](18, 0) NULL,"
                + " CONSTRAINT [PK_deposit] PRIMARY KEY CLUSTERED "
                + "("
                + "	[did] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql68 = "SET ANSI_NULLS ON";
            string sql69 = "SET QUOTED_IDENTIFIER ON";
            string sql70 = "CREATE TABLE [dbo].[card]("
                + "	[cid] [smallint] IDENTITY(1,1) NOT NULL,"
                + "	[cname] [nvarchar](20) NULL,"
                + "	[cdiscount] [float] NULL,"
                + "	[cmoney] [smallmoney] NULL,"
                + " CONSTRAINT [PK_card] PRIMARY KEY CLUSTERED "
                + "("
                + "	[cid] ASC"
                + ") ON [PRIMARY]"
                + ") ON [PRIMARY]";
            string sql71 = "SET ANSI_NULLS ON";
            string sql72 = "SET QUOTED_IDENTIFIER ON";
            string sql73 = "CREATE TABLE [dbo].[bb]("
                + "	[id] [int] IDENTITY(1,1) NOT NULL,"
                + "	[版本] [int] NULL"
                + ") ON [PRIMARY]";
            string sql74 = "ALTER TABLE [dbo].[subject] ADD  CONSTRAINT [DF_subject_senabled]  DEFAULT ((0)) FOR [senabled]";
            string sql75 = "ALTER TABLE [dbo].[post] ADD  CONSTRAINT [DF_post_psalary]  DEFAULT ((0)) FOR [psalary]";
            string sql76 = "ALTER TABLE [dbo].[pays] ADD  CONSTRAINT [DF_pays_pmoney]  DEFAULT ((0)) FOR [pmoney]";
            string sql77 = "ALTER TABLE [dbo].[pays] ADD  CONSTRAINT [DF_pays_pcash]  DEFAULT ((0)) FOR [pcash]";
            string sql78 = "ALTER TABLE [dbo].[pays] ADD  CONSTRAINT [DF_pays_pzero]  DEFAULT ((0)) FOR [pzero]";
            string sql79 = "ALTER TABLE [dbo].[pays] ADD  CONSTRAINT [DF_pays_pcall]  DEFAULT ((0)) FOR [pcall]";
            string sql80 = "ALTER TABLE [dbo].[member] ADD  CONSTRAINT [DF_member_mbalance]  DEFAULT ((0)) FOR [mbalance]";
            string sql81 = "ALTER TABLE [dbo].[member] ADD  CONSTRAINT [DF_member_mpoint]  DEFAULT ((0)) FOR [mpoint]";
            string sql82 = "ALTER TABLE [dbo].[items] ADD  CONSTRAINT [DF_items_isbonus]  DEFAULT ((0)) FOR [isbonus]";
            string sql83 = "ALTER TABLE [dbo].[items] ADD  CONSTRAINT [DF_items_isticheng]  DEFAULT ((0)) FOR [isticheng]";
            string sql84 = "ALTER TABLE [dbo].[deposit] ADD  CONSTRAINT [DF_deposit_dmoney]  DEFAULT ((0)) FOR [dmoney]";
            string sql85 = "ALTER TABLE [dbo].[deposit] ADD  CONSTRAINT [DF_deposit_dparentid]  DEFAULT ((0)) FOR [dparentid]";
            string sql86 = "INSERT INTO [BHairDB].[dbo].[LoginUser]"
                + "           ([LoginUserID]"
                + "           ,[LoginPassword])"
                + "     VALUES"
                + "           ('admin'"
                + "           ,'admin')";
            string sql87 = "INSERT INTO [BHairDB].[dbo].[bb]"
                + "           ([版本])"
                + "     VALUES (170314)";
                
            try
            {
                //string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
                string connstr = string.Format("Data Source={0};Initial Catalog=master;User ID={1};Password={2}", this.txtfwq.Text, this.txtsa.Text, this.txtpass.Text);
                SqlConnection cj = new SqlConnection(connstr);
                cj.Open();
                SqlCommand c1 = new SqlCommand(sql1, cj);
                c1.ExecuteNonQuery();
                SqlCommand c2 = new SqlCommand(sql2, cj);
                c2.ExecuteNonQuery();
                SqlCommand c3 = new SqlCommand(sql3, cj);
                c3.ExecuteNonQuery();
                SqlCommand c4 = new SqlCommand(sql4, cj);
                c4.ExecuteNonQuery();
                SqlCommand c5 = new SqlCommand(sql5, cj);
                c5.ExecuteNonQuery();
                SqlCommand c6 = new SqlCommand(sql6, cj);
                c6.ExecuteNonQuery();
                SqlCommand c7 = new SqlCommand(sql7, cj);
                c7.ExecuteNonQuery();
                SqlCommand c8 = new SqlCommand(sql8, cj);
                c8.ExecuteNonQuery();
                SqlCommand c9 = new SqlCommand(sql9, cj);
                c9.ExecuteNonQuery();
                SqlCommand c10 = new SqlCommand(sql10, cj);
                c10.ExecuteNonQuery();
                SqlCommand c11 = new SqlCommand(sql11, cj);
                c11.ExecuteNonQuery();
                SqlCommand c12 = new SqlCommand(sql12, cj);
                c12.ExecuteNonQuery();
                SqlCommand c13 = new SqlCommand(sql13, cj);
                c13.ExecuteNonQuery();
                SqlCommand c14 = new SqlCommand(sql14, cj);
                c14.ExecuteNonQuery();
                SqlCommand c15 = new SqlCommand(sql15, cj);
                c15.ExecuteNonQuery();
                SqlCommand c16 = new SqlCommand(sql16, cj);
                c16.ExecuteNonQuery();
                SqlCommand c17 = new SqlCommand(sql17, cj);
                c17.ExecuteNonQuery();
                SqlCommand c18 = new SqlCommand(sql18, cj);
                c18.ExecuteNonQuery();
                SqlCommand c19 = new SqlCommand(sql19, cj);
                c19.ExecuteNonQuery();
                SqlCommand c20 = new SqlCommand(sql20, cj);
                c20.ExecuteNonQuery();
                SqlCommand c21 = new SqlCommand(sql21, cj);
                c21.ExecuteNonQuery();
                SqlCommand c22 = new SqlCommand(sql22, cj);
                c22.ExecuteNonQuery();
                SqlCommand c23 = new SqlCommand(sql23, cj);
                c23.ExecuteNonQuery();
                SqlCommand c24 = new SqlCommand(sql24, cj);
                c24.ExecuteNonQuery();
                SqlCommand c25 = new SqlCommand(sql25, cj);
                c25.ExecuteNonQuery();
                SqlCommand c26 = new SqlCommand(sql26, cj);
                c26.ExecuteNonQuery();
                //SqlCommand c27 = new SqlCommand(sql27, cj);
                //c27.ExecuteNonQuery();
                SqlCommand c28 = new SqlCommand(sql28, cj);
                c28.ExecuteNonQuery();
                SqlCommand c29 = new SqlCommand(sql29, cj);
                c29.ExecuteNonQuery();
                SqlCommand c30 = new SqlCommand(sql30, cj);
                c30.ExecuteNonQuery();
                SqlCommand c31 = new SqlCommand(sql31, cj);
                c31.ExecuteNonQuery();
                SqlCommand c32 = new SqlCommand(sql32, cj);
                c32.ExecuteNonQuery();
                SqlCommand c33 = new SqlCommand(sql33, cj);
                c33.ExecuteNonQuery();
                SqlCommand c34 = new SqlCommand(sql34, cj);
                c34.ExecuteNonQuery();
                SqlCommand c35 = new SqlCommand(sql35, cj);
                c35.ExecuteNonQuery();
                SqlCommand c36 = new SqlCommand(sql36, cj);
                c36.ExecuteNonQuery();
                SqlCommand c37 = new SqlCommand(sql37, cj);
                c37.ExecuteNonQuery();
                SqlCommand c38 = new SqlCommand(sql38, cj);
                c38.ExecuteNonQuery();
                SqlCommand c39 = new SqlCommand(sql39, cj);
                c39.ExecuteNonQuery();
                SqlCommand c40 = new SqlCommand(sql40, cj);
                c40.ExecuteNonQuery();
                SqlCommand c41 = new SqlCommand(sql41, cj);
                c41.ExecuteNonQuery();
                SqlCommand c42 = new SqlCommand(sql42, cj);
                c42.ExecuteNonQuery();
                SqlCommand c43 = new SqlCommand(sql43, cj);
                c43.ExecuteNonQuery();
                SqlCommand c44 = new SqlCommand(sql44, cj);
                c44.ExecuteNonQuery();
                SqlCommand c45 = new SqlCommand(sql45, cj);
                c45.ExecuteNonQuery();
                SqlCommand c46 = new SqlCommand(sql46, cj);
                c46.ExecuteNonQuery();
                SqlCommand c47 = new SqlCommand(sql47, cj);
                c47.ExecuteNonQuery();
                SqlCommand c48 = new SqlCommand(sql48, cj);
                c48.ExecuteNonQuery();
                SqlCommand c49 = new SqlCommand(sql49, cj);
                c49.ExecuteNonQuery();
                SqlCommand c50 = new SqlCommand(sql50, cj);
                c50.ExecuteNonQuery();
                SqlCommand c51 = new SqlCommand(sql51, cj);
                c51.ExecuteNonQuery();
                SqlCommand c52 = new SqlCommand(sql52, cj);
                c52.ExecuteNonQuery();
                SqlCommand c53 = new SqlCommand(sql53, cj);
                c53.ExecuteNonQuery();
                SqlCommand c54 = new SqlCommand(sql54, cj);
                c54.ExecuteNonQuery();
                SqlCommand c55 = new SqlCommand(sql55, cj);
                c55.ExecuteNonQuery();
                SqlCommand c56 = new SqlCommand(sql56, cj);
                c56.ExecuteNonQuery();
                SqlCommand c57 = new SqlCommand(sql57, cj);
                c57.ExecuteNonQuery();
                SqlCommand c58 = new SqlCommand(sql58, cj);
                c58.ExecuteNonQuery();
                SqlCommand c59 = new SqlCommand(sql59, cj);
                c59.ExecuteNonQuery();
                SqlCommand c60 = new SqlCommand(sql60, cj);
                c60.ExecuteNonQuery();
                SqlCommand c61 = new SqlCommand(sql61, cj);
                c61.ExecuteNonQuery();
                SqlCommand c62 = new SqlCommand(sql62, cj);
                c62.ExecuteNonQuery();
                SqlCommand c63 = new SqlCommand(sql63, cj);
                c63 = new SqlCommand();
                SqlCommand c64 = new SqlCommand(sql64, cj);
                c64.ExecuteNonQuery();
                SqlCommand c65 = new SqlCommand(sql65, cj);
                c65.ExecuteNonQuery();
                SqlCommand c66 = new SqlCommand(sql66, cj);
                c66.ExecuteNonQuery();
                SqlCommand c67 = new SqlCommand(sql67, cj);
                c67.ExecuteNonQuery();
                SqlCommand c68 = new SqlCommand(sql68, cj);
                c68.ExecuteNonQuery();
                SqlCommand c69 = new SqlCommand(sql69, cj);
                c69.ExecuteNonQuery();
                SqlCommand c70 = new SqlCommand(sql70, cj);
                c70.ExecuteNonQuery();
                SqlCommand c71 = new SqlCommand(sql71, cj);
                c71.ExecuteNonQuery();
                SqlCommand c72 = new SqlCommand(sql72, cj);
                c72.ExecuteNonQuery();
                SqlCommand c73 = new SqlCommand(sql73, cj);
                c73.ExecuteNonQuery();
                SqlCommand c74 = new SqlCommand(sql74, cj);
                c74.ExecuteNonQuery();
                SqlCommand c75 = new SqlCommand(sql75, cj);
                c75.ExecuteNonQuery();
                SqlCommand c76 = new SqlCommand(sql76, cj);
                c76.ExecuteNonQuery();
                SqlCommand c77 = new SqlCommand(sql77, cj);
                c77.ExecuteNonQuery();
                SqlCommand c78 = new SqlCommand(sql78, cj);
                c78.ExecuteNonQuery();
                SqlCommand c79 = new SqlCommand(sql79, cj);
                c79.ExecuteNonQuery();
                SqlCommand c80 = new SqlCommand(sql80, cj);
                c80.ExecuteNonQuery();
                SqlCommand c81 = new SqlCommand(sql81, cj);
                c81.ExecuteNonQuery();
                SqlCommand c82 = new SqlCommand(sql82, cj);
                c82.ExecuteNonQuery();
                SqlCommand c83 = new SqlCommand(sql83, cj);
                c83.ExecuteNonQuery();
                SqlCommand c84 = new SqlCommand(sql84, cj);
                c84.ExecuteNonQuery();
                SqlCommand c85 = new SqlCommand(sql85, cj);
                c85.ExecuteNonQuery();
                SqlCommand c86 = new SqlCommand(sql86, cj);
                c86.ExecuteNonQuery();
                SqlCommand c87 = new SqlCommand(sql87, cj);
                c87.ExecuteNonQuery();
                cj.Close();
                MessageBox.Show("创建数据库成功");
            }catch(Exception ex)
            {
                
                MessageBox.Show("创建数据库出错"+ex.Message);
                
            }
        }
        //执行建库脚本
        //private string CommandLine(string name, params string[] args)
        //{
        //    return CommandLine(name, "", args);
        //}  
        // private string CommandLine(string name, string workingDirectory, params string[] args)  
        //{  
        //    string returnValue = "";  
  
        //    using (Process commandline = new Process())  
        //    {  
        //        try  
        //        {  
        //            commandline.StartInfo.UseShellExecute = false;  
        //            commandline.StartInfo.CreateNoWindow = true;  
        //            commandline.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;  
        //            commandline.StartInfo.RedirectStandardOutput = true;  
        //            commandline.StartInfo.FileName = name;  
        //            commandline.StartInfo.WorkingDirectory = workingDirectory;  
        //            //添加命令行参数  
        //            if (args.Length > 0) commandline.StartInfo.Arguments = string.Join(" ", args);  
        //            commandline.Start();  
        //            commandline.WaitForExit();  
        //            returnValue = commandline.StandardOutput.ReadToEnd();  
        //            commandline.Close();  
        //        }  
        //        catch  
        //        {  
        //            commandline.Dispose();  
        //            throw;  
        //        }  
        //    }  
  
        //    return returnValue;  
        //}
        ArrayList alSql = new ArrayList();
        public  ArrayList GetSqlFile(string varFileName, string dbname)
        {
            
            if (!File.Exists(varFileName))
            {
                return alSql;
            }
            StreamReader rs = new StreamReader(varFileName, System.Text.Encoding.Default);//注意编码
            string commandText = "";
            string varLine = "";
            while (rs.Peek() > -1)
            {
                varLine = rs.ReadLine();
                if (varLine == "")
                {
                    continue;
                }
                if (varLine != "GO" && varLine != "go")
                {
                    commandText += varLine;
                    commandText = commandText.Replace("@database_name=N'dbhr'", string.Format("@database_name=N'{0}'", dbname));
                    commandText += "\r\n";
                }
                else
                {
                    alSql.Add(commandText);
                    commandText = "";
                }
            }

            rs.Close();
            return alSql;
        }

        public static void ExecuteCommand(ArrayList varSqlList, string connString)
        {

            SqlConnection MyConnection = new SqlConnection(connString);
            MyConnection.Open();
            SqlTransaction varTrans = MyConnection.BeginTransaction();
            SqlCommand command = new SqlCommand();
            command.Connection = MyConnection;
            command.Transaction = varTrans;
            try
            {
                foreach (string varcommandText in varSqlList)
                {
                    command.CommandText = varcommandText;
                    command.ExecuteNonQuery();
                }
                varTrans.Commit();
            }
            catch (Exception ex)
            {
                varTrans.Rollback();
                throw ex;
            }
            finally
            {
                MyConnection.Close();
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process pr = new System.Diagnostics.Process();
                pr.StartInfo.FileName = "osql.exe ";
                pr.StartInfo.Arguments = "-U sa -P tudousi -d master -s 127.0.0.1 -i"+ AppDomain.CurrentDomain.BaseDirectory + "会员数据库创建.sql";
                pr.StartInfo.UseShellExecute = false;
                pr.StartInfo.RedirectStandardOutput = true;  //重定向输出

                pr.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//隐藏输出窗口
                pr.Start();

                System.IO.StreamReader sr = pr.StandardOutput;
                Console.WriteLine(sr.ReadToEnd());

                pr.WaitForExit();
                pr.Close();
                MessageBox.Show("创建数据库成功，请重新运行软件");
                Application.Exit();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }

        }
        #region 调用命令行工具

        /// <summary>
        /// 调用命令行工具
        /// </summary>
        /// <param name="name">命令行工具名称</param>
        /// <param name="args">可选命令行参数</param>
        /// <remarks>注意：所有命令行工具都必须保存于system32文件夹中</remarks>
        /// <returns></returns>
        //private string CommandLine(string name, params string[] args)
        //{
        //    return CommandLine(name, "", args);
        //}

        /// <summary>
        /// 调用命令行工具
        /// </summary>
        /// <param name="name">命令行工具名称</param>
        /// <param name="workingDirectory">设置工作目录</param>
        /// <param name="args">可选命令行参数</param>
        /// <remarks>注意：所有命令行工具都必须保存于system32文件夹中</remarks>
        /// <returns></returns>
        private string CommandLine(string name, string workingDirectory, params string[] args)
        {
            string returnValue = "";

            using (Process commandline = new Process())
            {
                try
                {
                    commandline.StartInfo.UseShellExecute = false;
                    commandline.StartInfo.CreateNoWindow = true;
                    commandline.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    commandline.StartInfo.RedirectStandardOutput = true;
                    commandline.StartInfo.FileName = name;
                    commandline.StartInfo.WorkingDirectory = workingDirectory;
                    //添加命令行参数
                    if (args.Length > 0) commandline.StartInfo.Arguments = string.Join(" ", args);
                    commandline.Start();
                    commandline.WaitForExit();
                    returnValue = commandline.StandardOutput.ReadToEnd();
                    commandline.Close();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    commandline.Dispose();
                    throw;
                }
            }

            return returnValue;
        }

        #endregion

    }
}

